<?php

function output_content_top_carousel($post_gallery_ids)
{
    if(!empty($post_gallery_ids)){
        $post_gallery_ids = explode(',',$post_gallery_ids);
    }

    $config = [
        'lazyLoad' => true,
        'autoplay' => true,
        'loop'     => true,
        'nav'      => true,
        'dots'     => true,
        'autoplayHoverPause' => true,
    ];

    ?>

    <div class="widget-slider owl-carousel owl-theme post-content gallery" data-config='<?php echo json_encode($config);?>'>
        <?php foreach ($post_gallery_ids as $item): ?>
            <div class="item gallery-item position-relative">
                <div class="widget-slider-item">
                    <a href="<?php echo wp_get_attachment_url($item);?>">
                        <img class="slider-img owl-lazy" data-src="<?php echo wp_get_attachment_url($item);?>">
                    </a>
                </div>
                <div class="position-absolute top-0 start-0 opacity-0 gallery-caption"><?php echo wp_get_attachment_caption($item);?></div>
            </div>
        <?php endforeach;?>
    </div>
    <?php
}

function output_faq_accordion($args){
    $theme_faqs = $args['home_faqs'];
    ?>
    <div class="row bg-white">
        <div class="container">
            <div class="text-center py-5">
                <h3 class="h2"><?php echo $args['title'];?></h3>
                <p class="mb-0 lead text-muted">以下问题列表是你可能会在本站的使用过程中遇到的常见问题，点击即可查看答案</p>
            </div>

            <ul class="row row-cols-2 row-cols-md-3 row-cols-lg-4 g-2 g-md-3 g-lg-4 list-unstyled accordion-list">
                <?php foreach ($theme_faqs as $item): ?>
                    <li class="col p-3 shadow-sm" style="cursor: pointer;">
                        <div class="question d-flex justify-content-between align-items-center">
                            <h5 class="fw-bold"><?php echo $item['question'];?></h5> <i class="fas fa-angle-down fs-3"></i>
                        </div>

                        <div class="answer">
                            <p><?php echo $item['answer'];?></p>
                        </div>
                    </li>
                <?php endforeach;?>
            </ul>
        </div>
    </div>
    <?php
}

function output_download_btn($user_id, $post_id) {
    // 用户是否已购买或者可免费获取
    $user_pay_post_status = get_user_pay_post_status($user_id, $post_id);
//是否免费资源并且需要登录后查看
    $is_user_login_get_status = $user_pay_post_status === true && empty($user_id);

// 用户是否已购买或者可免费获取
    $user_pay_post_status = get_user_pay_post_status($user_id, $post_id);
//是否免费资源并且需要登录后查看
    $is_user_login_get_status = $user_pay_post_status === true && empty($user_id);

//下载地址格式化
    $cao_downurl_new = get_post_meta( $post_id, 'cao_downurl_new', true);
    if (!empty($cao_downurl_new) && is_array($cao_downurl_new)) {
        foreach ($cao_downurl_new as $key => $item) {
            $cao_downurl_new[$key]['name'] = (!empty($item['name'])) ? trim($item['name']) : __('下载地址', 'ripro') . ($key + 1);
            $cao_downurl_new[$key]['pwd']  = (!empty($item['pwd'])) ? $item['pwd'] : '';
            $cao_downurl_new[$key]['url']  = get_post_endown_url($post_id, $key);
        }
    }else{
        $cao_downurl_new = array();
    }
    ?>

    <div class="ri-down-warp" data-resize="<?php echo esc_attr( $args['resize_position'] );?>">
        <?php if ($user_pay_post_status && !$is_user_login_get_status): ?>
            <div class="down-buy-warp">
                <?php if (!empty($cao_downurl_new)):?>
                    <div class="d-flex gap-2 mt-1">
                        <?php foreach ($cao_downurl_new as $item): ?>
                            <a target="_blank" href="<?php echo esc_attr($item['url']);?>" class="btn btn-success rounded-pill" rel="nofollow noopener noreferrer"><i class="fas fa-cloud-download-alt me-1"></i><?php echo $item['name'];?></a>
                        <?php endforeach; ?>
                        <?php output_btn_list($post_id); ?>
                    </div>
                <?php endif;?>
            </div>

        <?php else: ?>
            <div class="down-buy-warp">
                <?php if ($is_user_login_get_status):?>
                    <div class="buy-btns">
                        <a rel="nofollow noopener noreferrer" href="<?php echo esc_url( wp_login_url(get_current_url())); ?>" class="btn btn-info px-4 rounded-pill"><i class="far fa-user me-1"></i><?php _e('登录后下载', 'ripro');?></a>
                        <?php output_btn_list($post_id);?>
                    </div>

                <?php else:?>

                    <div class="buy-btns">
                        <button class="btn btn-danger px-4 rounded-pill js-pay-action" data-id="<?php echo $post_id;?>" data-type="1" data-info=""><i class="fab fa-shopify me-1"></i><?php _e('购买下载权限', 'ripro');?></button>
                        <?php output_btn_list($post_id);?>
                    </div>
                <?php endif;?>

            </div>

        <?php endif; ?>


        <div class="down-buy-info">
            <?php
            $cao_diy_btn = array_filter(explode('|', get_post_meta( $post_id, 'cao_diy_btn', true)));
            $btns = []; //DIY按钮

            if (!empty($cao_diy_btn)) {
                $btns[] = ['name'=>$cao_diy_btn[0],'url'=>$cao_diy_btn[1]];
            }
            ?>

            <?php if (!empty($btns)):?>
                <div class="d-flex gap-2 mt-1">
                    <?php foreach ($btns as $item): ?>
                        <a target="_blank" href="<?php echo esc_attr($item['url']);?>" class="btn btn-secondary-soft rounded-pill" rel="nofollow noopener noreferrer"><i class="fas fa-link me-1"></i><?php echo esc_attr($item['name']);?>123</a>
                    <?php endforeach; ?>
                    <?php output_btn_list($post_id); ?>
                </div>
            <?php endif;?>
        </div>

    </div>

    <?php
}

function output_btn_list($post_id){
    $theme_preview_url = get_post_meta($post_id, 'cao_demourl', true);
    $theme_official = get_post_meta($post_id, 'official_url', true);
    $dashboard_url = get_post_meta($post_id, 'dashboard_url', true);
    $virus_report_url = get_post_meta($post_id, 'virus_report_url', true);
    if(!empty($theme_official)):?>
        <a target="_blank" href="<?php echo $theme_official;?>" class="btn fs-6 mr-2 fw-bold btn-info px-4 rounded-pill" rel="nofollow noopener noreferrer"><i class="fas fa-eye me-1"></i>官网</a>
    <?php endif; if(!empty($theme_preview_url)):?>
        <a target="_blank" href="<?php echo $theme_preview_url;?>" class="btn fs-6 mr-2 fw-bold btn-success px-4 rounded-pill" rel="nofollow noopener noreferrer"><i class="fas fa-eye me-1"></i>前端预览</a>
    <?php endif; if(!empty($dashboard_url)):?>
        <a target="_blank" href="<?php echo $dashboard_url;?>" class="btn fs-6 mr-2 fw-bold btn-warning px-4 rounded-pill" rel="nofollow noopener noreferrer"><i class="fas fa-user me-1"></i>后台预览</a>
    <?php endif;if(!empty($virus_report_url)):?>
        <a target="_blank" href="<?php echo $virus_report_url;?>" class="btn fs-6 mr-2 fw-bold btn-info px-4 rounded-pill" rel="nofollow noopener noreferrer"><i class="fas fa-spider me-1"></i>病毒报告</a>
    <?php endif;
}

function output_price_table()
{
    $price_table = [
        ['title' => '普通会员','icon' => 'user', 'subtitle' => '可下载免费资源', 'price' => 0, 'span' => '永久', 'style' => 'secondary', 'checks' => ["far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-times-circle", "far fa-times-circle","far fa-times-circle"], 'benefits' => ['每天可下载5个免费资源', '可登录阅览免费教程', '可付费下载单个VIP资源', '可付费下载原创资源', '不能享受专业工单服务', '不能保证资源的有效性', '无专属VIP服务群资源']],
        ['title' => '试用VIP','icon' => 'crown', 'subtitle' => '试用VIP会员一天', 'price' => 29, 'span' => '7天', 'style' => 'info','checks' => ["far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-times-circle"], 'benefits' => ['一天可下载5次VIP资源', '可5折下载所有VIP资源', '可阅览所有教程', '可付费下载原创资源', '享受专业工单处理服务', '保证资源的有效性', '无专属VIP服务群资源']],
        ['title' => 'VIP包月','icon' => 'crown', 'subtitle' => '可下载全部VIP资源', 'price' => 99, 'span' => '月', 'style' => 'success', 'checks' => ["far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle"],'benefits' => ['每天可下载10次VIP资源','可下载所有VIP资源','可阅览所有教程', '下载原创资源享优惠价格', '保证资源的有效性', '享受专业处理工单服务', '专属VIP服务群资源']],
        ['title' => '永久会员','icon' => 'crown', 'subtitle' => '可下载免费资源', 'price' => 499, 'span' => '年','style' => 'warning', 'checks' => ["far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle", "far fa-check-circle"],'benefits' => ['每天可下载99次资源', '可阅览所有教程', '可下载所有原创资源', '可免费小幅修改主题及插件','保证资源的有效性', '可享优先专业处理工单', '专属VIP服务群资源']]
    ];
    ?>
    <section class="py-5 bg-white">
        <div class="container pb-4">
            <div class="text-center pb-5">
                <h3 class="h2">会员制授权，购买会员后可享全部资源</h3>
                <p class="mb-0 lead text-muted">本站所有资源均为官方原版正版下载，加入VIP会员即可享受会员期内的资源更新，无其他费用</p>
            </div>
            <div class="row row-cols-1 row-cols-md-4 g-3">
                <?php foreach ($price_table as $item): ?>
                    <div class="col">
                        <div class="card mb-3 border rounded-4 bg-white bg-opacity-50 shadow-sm">
                            <div class="p-3">
                                <div class="mb-4">
                                    <h3 class="fw-bold text-<?php echo $item['style'];?>"><i class="fas fa-<?php echo $item['icon'];?> me-1"></i><?php echo $item['title'];?></h3>
                                    <p class="mb-0"><?php echo $item['subtitle'];?></p>
                                    <h3 class="h1 mb-3 fw-bold mt-3"><?php echo $item['price'];?><span class="text-muted fw-normal" style=" font-size: .875rem;">/<?php echo $item['span'];?></span></h3>
                                </div>
                                <hr class="m-0">
                                <div class="mt-4">
                                    <h6 class="fw-bold mb-4">会员享受权益如下</h6>
                                    <ul class="list-unstyled mb-0">
                                        <?php foreach ($item['benefits'] as $index => $benefit):?>
                                            <li class="mb-1">
                                                <span class="text-<?php echo $item['style'];?> me-2"><i class="<?php echo $item['checks'][$index];?>"></i></span>
                                                <span class="text-muted"><?php echo $benefit;?></span>
                                            </li>
                                        <?php endforeach;?>
                                    </ul>
                                </div>
                                <div class="mt-4">
                                    <?php if(is_user_logged_in()):?>
                                        <a class="btn btn-<?php echo $item['style'];?> fw-bold" rel="nofollow noopener noreferrer" href="/login"><i class="fas fa-user mx-1"></i>立即登录</a>
                                    <?php else:?>
                                        <a class="btn btn-<?php echo $item['style'];?> fw-bold" rel="nofollow noopener noreferrer" href="/register"><i class="fas fa-link mx-1"></i>立即注册</a>
                                    <?php endif;?>

                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach;?>

            </div>

        </div>

    </section>
    <?php
}

function output_article_list_on_homepage(){
    ?>
    <div class="bg-white pb-5">
        <div class="container">
            <div class="row">
                <div class="col-12 col-md-6 col-lg-6">
                    <div class="card p-0 border shadow-sm">
                        <div class="card-header d-flex justify-content-center align-items-center text-white fw-bold fs-6 py-3 bg-dark">
                            <i class="fab fa-wordpress mx-1"></i>推荐WP使用教程
                        </div>
                        <div class="card-body">
                            <ul class="list-unstyled tuts-list my-2">
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>1</i><a href="" class="py-2">wordpress安装与使用终极教程(小白看这一篇就够了)</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>2</i><a href="" class="py-2">手把手教你建一个外贸独立站(利用woocommerce插件)</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>3</i><a href="" class="py-2">如何制作一个企业网站实战</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>4</i><a href="" class="py-2">如何制作一个会员制虚拟资源下载站（利用各种主题）</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>5</i><a href="" class="py-2">如何制作一视频会员制站</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>6</i><a href="" class="py-2">如何创建一个小型论坛（利用BBPRESS）</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>7</i><a href="" class="py-2">如何对wordpress进行性能调优和SEO优化</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>8</i><a href="" class="py-2">如何利用wordpress做一个多语言网站</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="col-12 col-md-6 col-lg-6">
                    <div class="card p-0 border shadow-sm">
                        <div class="card-header d-flex justify-content-center align-items-center text-white fw-bold fs-6 py-3 bg-dark">
                            <i class="fas fa-code mx-1"></i>推荐WP开发教程
                        </div>
                        <div class="card-body">
                            <ul class="list-unstyled tuts-list my-2">
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>1</i><a href="" class="py-2">如何开发wordpress主题详细教程（实战）</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>2</i><a href="" class="py-2">如何开发wordpress插件教程实战</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>3</i><a href="" class="py-2">如何利用wordpress极速开发一个API接口(利用wpgraph和相关插件)</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>4</i><a href="" class="py-2">如何利用Wpgraph与woocommerce和uni-app来极速创建小程序商城</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>5</i><a href="" class="py-2">如何利用Wpgraph和nextjs创建SEO优化的网站</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>6</i><a href="" class="py-2">如何利用后端选项输入框架极速构建后端输入项目(ACF和codestar framework)</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>7</i><a href="" class="py-2">从程序员的角度对wordpress进行性能调优</a></li>
                                <li class="position-relative d-flex align-items-center gap-2 fs-6"><i>8</i><a href="" class="py-2">Wordpress rest api入门与自定义开发</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
}